<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
	String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
%>
<html>
<head>
	<base href="<%=basePath%>">
<meta charset="UTF-8">
<link href="jquery/bootstrap_3.3.0/css/bootstrap.min.css" type="text/css" rel="stylesheet" />

<script type="text/javascript" src="jquery/jquery-1.11.1-min.js"></script>
<script type="text/javascript" src="jquery/bootstrap_3.3.0/js/bootstrap.min.js"></script>

	<script type="text/javascript">
		$(function () {
			queryAllDicType();
			quanxuanAndNo();
			toSavePage();
			toEditPage();
			delDictype();
		});

		function quanxuanAndNo() {

			/*$("#chkedAll").on("click",function () {

				$("#tBody input[type=checkbox]").prop("click",$("#chkedAll").prop("click"));
			});

			$("#tBody").on("click","input",function () {
				if($("#tBody input[type=checkbox]").size() == $("#tBody input[type=checkbox]:checked").size())
				{
					$("#chkedAll").prop("checked",true);
				}else{
					$("#chkedAll").prop("checked",false);
				}
			});*/

			$("#chkedAll").on("click",function () {

				$("#tBody input[type=checkbox]").prop("click",$("#chkedAll").prop("click"));
			});
			$("#tBody").on("click","input",function () {
				if($("#tBody input[type=checkbox]").size() == $("#tBody input[type=checkbox]:checked").size())
				{
					$("#chkedAll").prop("checked",true);
				}else{
					$("#chkedAll").prop("checked",false);
				}
			});

		}

		function queryAllDicType() {
			$.ajax({
				url:"settings/dictionary/type/queryAllDictypes.do",
				type:"get",
				success:function (data) {
					var dicTypeStr="";
					$.each(data,function (index,obj) {
						if(index%2 == 0)
						{
							dicTypeStr+="<tr class=\"active\">";
						}else{
							dicTypeStr+="<tr>";
						}
						dicTypeStr+="<td><input type=\"checkbox\" value=\'"+obj.code+"\' /></td>";
						dicTypeStr+="<td>"+(index+1)+"</td>";
						dicTypeStr+="<td>"+obj.code+"</td>";
						dicTypeStr+="<td>"+obj.name+"</td>";
						dicTypeStr+="<td>"+obj.description+"</td>";
						dicTypeStr+="</tr>";

					});
					$("#tBody").html(dicTypeStr);
				}
			});
		}
		
		function toSavePage() {
			$("#createDicTypeBtn").on("click",function () {
				window.location.href = "settings/dictionary/type/toSavePage.do";
			});
		}

		function toEditPage() {
			$("#editDicTypeBtn").on("click",function () {

				var checkedSize = $("#tBody input[type=checkbox]:checked").size();
				if(checkedSize  != 1)
				{
					alert("必须选中一条数据！");
					return;
				}
				var checkedVal = $("#tBody input[type=checkbox]:checked").val();
				window.location.href="settings/dictionary/type/toEditPage.do?code="+checkedVal;
			});
		}

		function delDictype() {
			$("#deleteDicTypeBtn").on("click",function () {
				var delCodes = $("#tBody input[type=checkbox]:checked");
				if(delCodes.size() == 0)
				{
					alert("请您先选中一条数据！");
					return;
				}
				if(confirm("真的要删除吗？"))
				{
					var codes="";
					$.each(delCodes,function (index,code) {
						codes+="code="+$(code).val()+"&";
					});
					codes = codes.substring(0,codes.length-1);

					$.ajax({
						url:"settings/dictionary/type/delDictype.do",
						type:"post",
						data:codes,
						success:function (data) {
							if(data.code ==1)
							{
								alert("删除成功！");
								queryAllDicType();
							}else{
								alert(data.message);
							}
						}

					});
				}

			});

		}

		
	</script>

</head>
<body>

	<div>
		<div style="position: relative; left: 30px; top: -10px;">
			<div class="page-header">
				<h3>字典类型列表</h3>
			</div>
		</div>
	</div>
	<div class="btn-toolbar" role="toolbar" style="background-color: #F7F7F7; height: 50px; position: relative;left: 30px;">
		<div class="btn-group" style="position: relative; top: 18%;">
		  <button id="createDicTypeBtn" type="button" class="btn btn-primary"><span class="glyphicon glyphicon-plus"></span> 创建</button>
		  <button id="editDicTypeBtn" type="button" class="btn btn-default"><span class="glyphicon glyphicon-edit"></span> 编辑</button>
		  <button id="deleteDicTypeBtn" type="button" class="btn btn-danger"><span class="glyphicon glyphicon-minus"></span> 删除</button>
		</div>
	</div>
	<div style="position: relative; left: 30px; top: 20px;">
		<table class="table table-hover">
			<thead>
				<tr style="color: #B3B3B3;">
					<td><input type="checkbox" id="chkedAll"/></td>
					<td>序号</td>
					<td>编码</td>
					<td>名称</td>
					<td>描述</td>
				</tr>
			</thead>
			<tbody id="tBody">
				<tr class="active">
					<td><input type="checkbox" /></td>
					<td>1</td>
					<td>sex</td>
					<td>性别</td>
					<td>性别</td>
				</tr>
				<tr class="active">
					<td><input type="checkbox" /></td>
					<td>2</td>
					<td>appellation</td>
					<td>称呼</td>
					<td>称呼</td>
				</tr>
			</tbody>
		</table>
	</div>
	
</body>

</html>